package com.yuwubao.uav.tool.controller;

import com.yuwubao.framework.bean.JsonResult;
import com.yuwubao.framework.bean.PageData;
import com.yuwubao.framework.loginFilter.LoginRequired;
import com.yuwubao.uav.tool.service.PaternerService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.Min;

/**
 *合作单位管理
 */
@RestController
@RequestMapping("/bk")
public class PaternerController {

    private static final Logger logger = LogManager.getLogger(PaternerController.class);

    @Resource
    private PaternerService paternerService;

    @LoginRequired
    @PostMapping("/addPaterner")
    @ApiOperation(value = "新增合作单位")
    public JsonResult addPaterner(
            @ApiParam(value = "合作单位名称", required = true) @RequestParam("name") String name,
            @ApiParam(value = "合作单位图片") @RequestParam(value = "img",required = false) String img,
            @ApiParam(value = "连接地址") @RequestParam(value = "url",required = false) String url,
            @ApiParam(value = "备注") @RequestParam(value = "remark",required = false) String remark,
            @ApiParam(value = "合作单位类型ID") @RequestParam(value = "paternerTypeId",required = false) String paternerTypeId
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("name", name);
            pageData.put("img", img);
            pageData.put("url", url);
            pageData.put("remark", remark);
            pageData.put("paternerTypeId", paternerTypeId);

            int count = paternerService.getPaternerByName(pageData);
            if (count > 0) {
                result.setFailure("单位名称已经存在");
                return result;
            }

            paternerService.addPaterner(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/updatePaterner")
    @ApiOperation(value = "更新合作单位")
    public JsonResult updatePaterner(
            @ApiParam(value = "合作单位ID", required = true) @RequestParam("id") String id,
            @ApiParam(value = "合作单位名称", required = true) @RequestParam("name") String name,
            @ApiParam(value = "合作单位图片") @RequestParam(value = "img",required = false) String img,
            @ApiParam(value = "连接地址") @RequestParam(value = "url",required = false) String url,
            @ApiParam(value = "备注") @RequestParam(value = "remark",required = false) String remark,
            @ApiParam(value = "合作单位类型ID") @RequestParam(value = "paternerTypeId",required = false) String paternerTypeId
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("id", id);
            pageData.put("name", name);
            pageData.put("img", img);
            pageData.put("url", url);
            pageData.put("remark", remark);
            pageData.put("paternerTypeId", paternerTypeId);

            int count = paternerService.getPaternerByName(pageData);
            if (count > 0) {
                result.setFailure("单位名称已经存在");
                return result;
            }

            paternerService.updatePaterner(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @PostMapping("/deletePaterner")
    @ApiOperation(value = "删除合作单位")
    public JsonResult deletePaterner(
            @ApiParam(value = "合作单位ID，多个用逗号分隔", required = true) @RequestParam("ids") String ids
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("ids", ids);

            paternerService.deletePaterner(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @PostMapping("/disableOrEnablePaterner")
    @ApiOperation(value = "启用或者禁用合作单位")
    public JsonResult disableOrEnablePaterner(
            @ApiParam(value = "合作单位ID,多个用逗号分隔", required = true) @RequestParam("ids") String ids,
            @ApiParam(value = "0:禁用  1：启用", required = true) @RequestParam("state") String state
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("ids", ids);
            pageData.put("state", state);

            paternerService.disableOrEnablePaterner(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @GetMapping("/listPaterner")
    @ApiOperation(value = "获取合作单位列表")
    public JsonResult listPaterner(
            @ApiParam(value = "当前页", required = true) @Min(1) @Length(max = 8) @NotEmpty @RequestParam("page") String page,
            @ApiParam(value = "当前页数量", required = true) @Min(10) @Length(max = 3) @NotEmpty @RequestParam("pageSize") String pageSize,
            @ApiParam(value = "搜索关键字") @RequestParam(value = "searchKey",required = false) String searchKey
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("searchKey", searchKey);
            pageData.put("begin", (Integer.valueOf(page) - 1) * Integer.valueOf(pageSize));
            pageData.put("pageSize", Integer.valueOf(pageSize));

            result.setResult(paternerService.listPaterner(pageData));
            result.setResultNum(paternerService.listPaternerCount(pageData));

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @PostMapping("/addPaternerType")
    @ApiOperation(value = "新增合作单位类型")
    public JsonResult addPaternerType(
            @ApiParam(value = "合作单位类型,多个用逗号分隔", required = true) @RequestParam("names") String names
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("names", names);

            paternerService.addPaternerType(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/updatePaternerType")
    @ApiOperation(value = "更新合作单位类型")
    public JsonResult updatePaternerType(
            @ApiParam(value = "合作单位类型ID", required = true) @RequestParam("id") String id,
            @ApiParam(value = "合作单位类型名称", required = true) @RequestParam("name") String name
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("id", id);
            pageData.put("name", name);

            int count = paternerService.getPaternerTypeByName(pageData);
            if(count > 0) {
                result.setFailure("该名称已经存在");
                return result;
            }
            paternerService.updatePaternerType(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/deletePaternerType")
    @ApiOperation(value = "删除合作单位类型")
    public JsonResult deletePaternerType(
            @ApiParam(value = "合作单位类型ID，多个用逗号分隔", required = true) @RequestParam("ids") String ids
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("ids", ids);

            paternerService.deletePaternerType(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @PostMapping("/disableOrEnablePaternerType")
    @ApiOperation(value = "启用或者禁用合作单位类型")
    public JsonResult disableOrEnablePaternerType(
            @ApiParam(value = "合作单位ID,多个用逗号分隔", required = true) @RequestParam("ids") String ids,
            @ApiParam(value = "0:禁用  1：启用", required = true) @RequestParam("state") String state
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("ids", ids);
            pageData.put("state", state);

            paternerService.disableOrEnablePaternerType(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @GetMapping("/listPaternerType")
    @ApiOperation(value = "获取合作单位类型列表")
    public JsonResult listPaternerType(
            @ApiParam(value = "当前页", required = true) @Min(1) @Length(max = 8) @NotEmpty @RequestParam("page") String page,
            @ApiParam(value = "当前页数量", required = true) @Min(10) @Length(max = 3) @NotEmpty @RequestParam("pageSize") String pageSize,
            @ApiParam(value = "搜索关键字") @RequestParam(value = "searchKey",required = false) String searchKey
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("searchKey", searchKey);
            pageData.put("begin", (Integer.valueOf(page) - 1) * Integer.valueOf(pageSize));
            pageData.put("pageSize", Integer.valueOf(pageSize));

            result.setResult(paternerService.listPaternerType(pageData));
            result.setResultNum(paternerService.listPaternerTypeCount(pageData));

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @GetMapping("/listPaternerTypeAll")
    @ApiOperation(value = "获取合作单位类型列表所有")
    public JsonResult listPaternerTypeAll(
            @ApiParam(value = "搜索关键字") @RequestParam(value = "searchKey",required = false) String searchKey
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("searchKey", searchKey);

            result.setResult(paternerService.listPaternerTypeAll(pageData));

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }



}
